<template>
  <j-modal
    :title='title'
    :width='width'
    :visible='visible'
    switchFullscreen
    :okButtonProps="{ class:{'jee-hidden': disableSubmit} }"
    @cancel='handleCancel'
    @ok='handleCancel'
    cancelText='关闭'>
    <template v-if="type !== 'a4' && type !== 'b1' && type !== 'b9'">
      <a-table
        ref='table'
        size='middle'
        :scroll='{x:1175,y:600}'
        bordered
        :rowKey='(record,index)=>{return index}'
        :columns='columns'
        :dataSource='dataSource'
        :pagination='false'
        :loading='loading'
        class='j-table-force-nowrap'
      />
    </template>
    <template v-else>
      <a-tabs v-model='activeKey' :default-active-key='1' @change='handleTabsChange'>
        <a-tab-pane v-for='(item,index) in tabsData' :tab='item.title' :key='index+1'>
          <a-table
            ref='table'
            size='middle'
            :scroll='{x:1175,y:600}'
            bordered
            :rowKey='(record,index)=>{return index}'
            :columns="type === 'b1'|| type === 'b9' || type === 'a4'? columns[item.type] : columns"
            :dataSource='filterDataSource(index)'
            :pagination='false'
            :loading='loading'
            class='j-table-force-nowrap'
          />
        </a-tab-pane>
      </a-tabs>
    </template>
  </j-modal>
</template>

<script>
import { JeecgListMixin } from '@/mixins/JeecgListMixin'
import { getAction } from '@api/manage'
import { filterDictTextByCache } from '@comp/dict/JDictSelectUtil'

export default {
  name: 'HfOperationCostApplyModal',
  mixins: [JeecgListMixin],
  data() {
    return {
      title: '明细',
      width: '90%',
      type: '',
      activeKey: 1,
      visible: false,
      disableSubmit: true,
      columns: [],
      tabsData: [],
      disableMixinCreated: true,
      url: {
        list: '/ps/hfPsSalary/details'
      }
    }
  },
  methods: {
    open(type, salTime) {
      this.visible = true
      this.type = type
      this.setColumns(type)
      this.loadData(type, salTime)
      if (type === 'a4') {
        this.tabsData = [{ title: '非经营费用', type: 'costList' }, { title: '备用金', type: 'costList' },
          { title: '结息/其他', type: 'taxesList' }, { title: '其他', type: 'costList' }]
      } else if (type === 'b1') {
        this.tabsData = [{ title: '采购付款', type: 'paymentList' }, { title: '成本报销', type: 'oaReimbursementList' }]
      } else if (type === 'b9') {
        this.tabsData = [{ title: '日常运营', type: 'costList' }, { title: '采购费B', type: 'costList' },
          { title: '财税费', type: 'taxesList' }, { title: '财务费', type: 'costList' }]
      }
    },
    loadData(type, salTime) {
      getAction(this.url.list, { type, salTime }).then(res => {
        if (res.success) {
          this.dataSource = res.result
        }
      })
    },
    filterDataSource(index) {
      if (this.dataSource.constructor === Object) {
        if (this.type === 'b1') {
          for (let key in this.dataSource) {
            if (key === this.tabsData[index].type) {
              return this.dataSource[key]
            }
          }
        }
        if (this.type === 'b9') {
          for (let key in this.dataSource) {
            if (index != 2) {
              if (key === this.tabsData[index].type) {
                return this.dataSource[key].filter(item => {
                  return index === 0 ? item.costType !== '100' && item.costType !== '30' : index === 1 ? item.costType === '100' : item.costType === '30'
                })
              }
            } else {
              if (key === this.tabsData[index].type) {
                return this.dataSource[key].filter(item => {
                  return item.taxesTypeSmall === '3' || item.taxesTypeSmall === '4' || item.taxesTypeSmall === '5'
                })
              }
            }
          }
        }
        if (this.type === 'a4') {
          for (let key in this.dataSource) {
            if (index != 2) {
              if (key === this.tabsData[index].type) {
                return this.dataSource[key].filter(item => {
                  return index === 0 ? item.costType === '21' : index === 1 ? item.costType === '10' : item.costType !== '10' && item.costType !== '21'
                })
              }
            } else {
              if (key === this.tabsData[index].type) {
                return this.dataSource[key].filter(item => {
                  return item.taxesTypeSmall === '1' || item.taxesTypeSmall === '2'
                })
              }
            }
          }
        }
      } else {

      }
    },
    handleCancel() {
      this.visible = false
    },
    handleTabsChange(key) {
      this.activeKey = key
    },
    setColumns(type) {
      this.columns = []
      if (type === 'a1') {
        this.columns.push(
          {
            title: '#',
            dataIndex: '',
            key: 'rowIndex',
            width: 60,
            align: 'center',
            customRender: function(t, r, index) {
              return parseInt(index) + 1
            }
          },
          {
            title: '合同编号',
            dataIndex: 'contractNum'
          },
          {
            title: '合同名称',
            dataIndex: 'contractName'
          },
          {
            title: '客户名称',
            dataIndex: 'customerName'
          },
          {
            title: '回款条件',
            dataIndex: 'returnCondition',
            customRender: (t) => {
              return filterDictTextByCache('icm_payment_collection_condition', t) || '--'
            }
          },
          {
            title: '实际回款时间',
            dataIndex: 'actualTime'
          },
          {
            title: '实际回款金额',
            dataIndex: 'actualMoney'
          }
        )
      }
      if (type === 'a2' || type === 'b6' || type === 'b10' || type === 'c1' || type === 'c2' || type === 'd1' || type === 'e1' || type === 'f1') {
        this.columns.push(
          {
            title: '#',
            dataIndex: '',
            key: 'rowIndex',
            width: 60,
            align: 'center',
            customRender: function(t, r, index) {
              return parseInt(index) + 1
            }
          },
          {
            title: '费用所属',
            dataIndex: 'auditGrade',
            width: 100,
            customRender: (t) => {
              return filterDictTextByCache('audit_grade', t) || '--'
            }
          },
          {
            title: type === 'e1' || type === 'b10' || type === 'f1' ? '费用类别' : '财税小类',
            dataIndex: type === 'e1' || type === 'b10' || type === 'f1' ? 'costType' : 'taxesTypeSmall',
            width: 200,
            customRender: (t) => {
              return filterDictTextByCache(type === 'e1' || type === 'b10' || type === 'f1' ? 'cost_type' : 'taxes_type_small', t) || '--'
            }
          },
          {
            title: '采购名称',
            width: 275,
            dataIndex: 'purchaseName'
          },
          {
            title: '费用说明',
            width: 500,
            dataIndex: type === 'e1' || type === 'b10' || type === 'f1' ? 'costExplain' : 'taxesExplain'
          },
          {
            title: '金额',
            width: 100,
            dataIndex: type === 'e1' || type === 'b10' || type === 'f1' ? 'money' : 'amount'
          }
        )
      }
      if (type === 'a3') {
        this.columns.push(
          {
            title: '#',
            dataIndex: '',
            key: 'rowIndex',
            width: 60,
            align: 'center',
            customRender: function(t, r, index) {
              return parseInt(index) + 1
            }
          },
          {
            title: '项目名称',
            dataIndex: 'projectName'
          },
          {
            title: '客户名称',
            dataIndex: 'customerName'
          },
          {
            title: '退回时间',
            dataIndex: 'returnDate'
          },
          {
            title: '退回金额',
            dataIndex: 'returnMoney'
          }
        )
      }
      if (type === 'a4' || type === 'b9') {
        this.columns = {
          'costList': [
            {
              title: '#',
              dataIndex: '',
              key: 'rowIndex',
              width: 60,
              align: 'center',
              customRender: function(t, r, index) {
                return parseInt(index) + 1
              }
            },
            {
              title: '费用所属',
              dataIndex: 'auditGrade',
              width: 100,
              customRender: (t) => {
                return filterDictTextByCache('audit_grade', t) || '--'
              }
            },
            {
              title: '费用类别',
              dataIndex: 'costType',
              width: 200,
              customRender: (t) => {
                return filterDictTextByCache('cost_type', t) || '--'
              }
            },
            {
              title: '采购名称',
              width: 275,
              dataIndex: 'purchaseName'
            },
            {
              title: '费用说明',
              width: 500,
              dataIndex: 'costExplain'
            },
            {
              title: '金额',
              width: 100,
              dataIndex: 'money'
            }
          ],
          'taxesList': [
            {
              title: '#',
              dataIndex: '',
              key: 'rowIndex',
              width: 60,
              align: 'center',
              customRender: function(t, r, index) {
                return parseInt(index) + 1
              }
            },
            {
              title: '费用所属',
              dataIndex: 'auditGrade',
              width: 100,
              customRender: (t) => {
                return filterDictTextByCache('audit_grade', t) || '--'
              }
            },
            {
              title: '财税小类',
              dataIndex: 'taxesTypeSmall',
              width: 200,
              customRender: (t) => {
                return filterDictTextByCache('taxes_type_small', t) || '--'
              }
            },
            {
              title: '采购名称',
              width: 275,
              dataIndex: 'purchaseName'
            },
            {
              title: '费用说明',
              width: 500,
              dataIndex: 'taxesExplain'
            },
            {
              title: '金额',
              width: 100,
              dataIndex: 'amount'
            }
          ]
        }
      }
      if (type === 'b1') {
        this.columns = {
          'paymentList': [
            {
              title: '#',
              dataIndex: '',
              key: 'rowIndex',
              width: 60,
              align: 'center',
              customRender: function(t, r, index) {
                return parseInt(index) + 1
              }
            },
            {
              title: '采购合同编号',
              dataIndex: 'purchaseNum'
            },
            {
              title: '项目名称',
              dataIndex: 'projectName'
            },
            {
              title: '客户名称',
              dataIndex: 'customerName'
            },
            {
              title: '实际付款时间',
              dataIndex: 'actualTime'
            },
            {
              title: '实际付款金额',
              dataIndex: 'actualMoney'
            }
          ],
          'oaReimbursementList': [
            {
              title: '#',
              dataIndex: '',
              key: 'rowIndex',
              width: 60,
              align: 'center',
              customRender: function(t, r, index) {
                return parseInt(index) + 1
              }
            },
            {
              title: '申请人',
              dataIndex: 'createBy'
            },
            {
              title: '费用所属',
              dataIndex: 'auditGrade',
              customRender: (t) => {
                return filterDictTextByCache('audit_grade', t) || '--'
              }
            },
            {
              title: '报销类别',
              dataIndex: 'type',
              customRender: (t) => {
                return filterDictTextByCache('hf_oa_reimbursement_type', t) || '--'
              }
            },
            {
              title: '商机名称',
              dataIndex: 'opportunityName'
            },
            {
              title: '项目简称',
              dataIndex: 'projectName'
            },
            {
              title: '发放时间',
              dataIndex: 'issueTime'
            },
            {
              title: '金额',
              dataIndex: 'total'
            }
          ]
        }
      }
      if (type === 'b2') {
        this.columns.push(
          {
            title: '#',
            dataIndex: '',
            key: 'rowIndex',
            width: 60,
            align: 'center',
            customRender: function(t, r, index) {
              return parseInt(index) + 1
            }
          },
          {
            title: '月份',
            dataIndex: 'salTime'
          },
          {
            title: '员工姓名',
            dataIndex: 'realname'
          },
          {
            title: '薪酬分类',
            dataIndex: 'salaryType',
            customRender: (t) => {
              return filterDictTextByCache('salary_type', t) || '--'
            }
          },
          {
            title: '职工薪酬',
            dataIndex: 'partSalary'
          }
        )
      }
      if (type === 'b3') {
        this.columns.push(
          {
            title: '#',
            dataIndex: '',
            key: 'rowIndex',
            width: 60,
            align: 'center',
            customRender: function(t, r, index) {
              return parseInt(index) + 1
            }
          },
          {
            title: '月份',
            dataIndex: 'salTime'
          },
          {
            title: '员工姓名',
            dataIndex: 'realname'
          },
          {
            title: '薪酬分类',
            dataIndex: 'salaryType',
            customRender: (t) => {
              return filterDictTextByCache('salary_type', t) || '--'
            }
          },
          {
            title: '职工薪酬',
            dataIndex: 'partSalary'
          }
        )
      }
      if (type === 'b4') {
        this.columns.push(
          {
            title: '#',
            dataIndex: '',
            key: 'rowIndex',
            width: 60,
            align: 'center',
            customRender: function(t, r, index) {
              return parseInt(index) + 1
            }
          },
          {
            title: '月份',
            dataIndex: 'salTime'
          },
          {
            title: '员工姓名',
            dataIndex: 'realname'
          },
          {
            title: '薪酬分类',
            dataIndex: 'salaryType',
            customRender: (t) => {
              return filterDictTextByCache('salary_type', t) || '--'
            }
          },
          {
            title: '职工薪酬',
            dataIndex: 'partSalary'
          }
        )
      }
      if (type === 'b7') {
        this.columns.push(
          {
            title: '#',
            dataIndex: '',
            key: 'rowIndex',
            width: 60,
            align: 'center',
            customRender: function(t, r, index) {
              return parseInt(index) + 1
            }
          },
          {
            title: '申请人',
            dataIndex: 'createBy'
          },
          {
            title: '费用所属',
            dataIndex: 'auditGrade',
            customRender: (t) => {
              return filterDictTextByCache('audit_grade', t) || '--'
            }
          },
          {
            title: '报销类别',
            dataIndex: 'type',
            customRender: (t) => {
              return filterDictTextByCache('hf_oa_reimbursement_type', t) || '--'
            }
          },
          {
            title: '商机名称',
            dataIndex: 'opportunityName'
          },
          {
            title: '项目简称',
            dataIndex: 'projectName'
          },
          {
            title: '发放时间',
            dataIndex: 'issueTime'
          },
          {
            title: '金额',
            dataIndex: 'total'
          }
        )
      }
      if (type === 'b8') {
        this.columns.push(
          {
            title: '#',
            dataIndex: '',
            key: 'rowIndex',
            width: 60,
            align: 'center',
            customRender: function(t, r, index) {
              return parseInt(index) + 1
            }
          },
          {
            title: '项目名称',
            dataIndex: 'projectName'
          },
          {
            title: '客户名称',
            dataIndex: 'customerName'
          },
          {
            title: '支付时间',
            dataIndex: 'issueTime'
          },
          {
            title: '支付金额',
            dataIndex: 'deposit'
          },
          {
            title: '中标服务费',
            dataIndex: 'biddingMoney'
          }
        )
      }
      if (this.columns.constructor === Object) {
        for (let key in this.columns) {
          this.columns[key].forEach(item => {
            item.align = 'center'
            item.ellipsis = true
          })
        }
      } else {
        this.columns.forEach(item => {
          item.align = 'center'
          item.ellipsis = true
        })
      }
    }
  }
}
</script>
<style lang='less' scoped>
/deep/ .ant-modal {
  top: 60px;
}

</style>